<include file="Index/header" />
<body class="body">
<include file="Index/picture" />
	<div class="login_box">
	<div style="text-align:center;">
    	<p style="margin-bottom:5px;"><span style="float:none;">请填写真实的注册信息</span><br />
    		<span style="color:#f00;float:none;">非MBA/EMBA校友请勿注册</span><br />
    		<span style="float:none;">已注册校友请 <a style="float:none;" href="/Index/login?community_id={$Think.get.community_id}&invite_id={$Think.get.invite_id}">直接登录</a></span>
    	<!-- <a href="/Index/login?community_id={$Think.get.community_id}&invite_id={$Think.get.invite_id}">直接登录</a>；第一次加入的MBA/EMBA校友请注册；非MBA/EMBA校友请勿注册。 -->
    	</p>
    </div>
    	<input type="text" id="user_phone" placeholder="手机号（必填）"/>
        <div class="count">
        	<input type="text" id="verify_code" placeholder="验证码（必填）"/>
            <button type="button" onclick="getVerify();"><span id="send">发送验证码</span></button>
        </div>
        <input type="password" id="user_password" placeholder="密码(用于客户端登录)"/>
        <input type="password" id="user_password_r" placeholder="确认密码（必填）"/>
        <input type="text" id="user_name" placeholder="姓名（必填）"/>
        <select id="school" name="user_school" onchange="changeSchool();">
		      	<volist name="school_list" id="vo" key="k">
			      	<option value="{$vo.school_id}">{$vo.first} {$vo.school_name}</option>
			 	</volist>
		</select>
		<select id="grade_id" name="grade_id">
			<option value="0">年级(必选)</option>
		</select>
        <button type="button" class="submit" onclick="checkLogin()" id="tijiao">提交</button>
        <button type="button" class="submit" id="notijiao">提交中...</button>
    </div>
    <include file="Index/down" />
</body>
</html>
<script type="text/javascript">
	$(function($) {
		changeSchool();
		$("#notijiao").hide();
		$("#tijiao").show();
	});
	function changeSchool(){
		var school_id = $('select[name="user_school"] option:selected').val();
		$("#grade_id").load("/Index/getGrade?school_id="+school_id+"&random="+Math.random());
	}
	
	/*
     *验证码
     */
    function getVerify(){
        var user_phone = $("#user_phone").val();

        if(!isPhone(user_phone)){
            alert("请输入正确手机号");
            return false;
        }
        var post_data={'user_phone':user_phone,'type':1};//type=1 注册，2找回密码
        $.ajax({
            type: "POST",
            url:"/Index/getVerify",
            data: post_data,
            success: function(data){
                if(data.status == '0'){
                    alert(data.info);
                    RemainTime();
                }else{
                    alert(data.info);
                    return false;
                }

            },
            dataType:"json"
        });
        
 
    }
    function isPhone(str){
        return str.match(RegExp("^1[34578][0-9]{9}$","g"));
    }
    
    function checkPwd(str){
        return str.length > 5 && str.length<15 && !(/\s+/.test(str));
    }
    function checkVerify(str){
        return str.length==4;
    }
    var iTime = 59;
    var Account;
    function RemainTime(){
        G('send').disabled = true;
        var iSecond,sSecond="",sTime="";
        if (iTime >= 0){
            iSecond = parseInt(iTime%60);
            iMinute = parseInt(iTime/60)
            if (iSecond >= 0){
                if(iMinute>0){
                    sSecond = iMinute + "分" + iSecond + "秒";
                }else{
                    sSecond = iSecond + "秒";
                }
            }
            sTime=sSecond;
            if(iTime==0){
                clearTimeout(Account);
                sTime='发送验证码';
                iTime = 59;
                G('send').disabled = false;
            }else{
                Account = setTimeout("RemainTime()",1000);
                iTime=iTime-1;
            }
        }else{
            sTime='没有倒计时';
        }
        G('send').innerHTML = sTime;
    }
    function G(id){
        return document.getElementById(id);
    }
	function checkLogin() {
        var user_phone = $("#user_phone").val();
        var verify_code = $("#verify_code").val();
        var user_password = $("#user_password").val();
        var user_password_r = $("#user_password_r").val();
        var user_name = $("#user_name").val();
        var school_id = $('select[name="user_school"] option:selected').val();
        var grade_id = $('select[name="grade_id"] option:selected').val();
/* console.log(content);return false; */
        
        if(!isPhone(user_phone)){
            alert("请输入正确手机号");
            return false;
        }
        if(!checkVerify(verify_code)){
            alert("验证码不正确");
            return false;
        }
        if (user_password) {
			var reg_phone = /^[A-Za-z0-9]{6,15}$/;
			if (!reg_phone.exec(user_password)) {
				alert("密码由6-20个长度的数字或字母组成！");
				return false;
			}
		}
        
        if(user_password == '' || user_password == undefined){
        	alert("密码不能为空");
			return false;
		}
        
        if(user_password != user_password_r){
        	alert("请保证两次密码填写一致");
			return false;
        }
        
        if(user_name == ''){
            alert("请输入用户名");
            return false;
        }
        if(school_id == '' || school_id == undefined || school_id == '0'){
            alert("请选择学校");
            return false;
        }
        //console.log(grade_id);return false;
        if(grade_id == '' || grade_id == undefined || grade_id == 0){
            alert("请选择学届");
            return false;
        }
        $("#notijiao").show();
        $("#tijiao").hide();
        var post_data={
       		'user_phone':user_phone,
       		'verify_code':verify_code,
       		'user_password':user_password,
       		'user_name' : user_name,
       		'school_id' : school_id,
       		'grade_id' : grade_id
        };
        $.ajax({
            type: "POST",
            url:"/Index/reg",
            data: post_data,
            success: function(data){
                if(data.status == '0'){
                	//调用加入社群接口的方法
                	var community_id = "{$Think.get.community_id}";
    				joinCommunity(community_id);
                }else{
                	$("#notijiao").hide();
                	$("#tijiao").show();
                    alert(data.info);
                }

            },
            dataType:"json"
        });
    }
	
	//加入社群接口的方法
	function joinCommunity(community_id){
		var host = 'http://'+window.location.host;
		invite_id = "{$Think.get.invite_id}";
		var post_data={'community_id':community_id,'invite_id':invite_id};
		$.ajax({
			type: "POST",
			url:"/Index/joinCommunity",
			data: post_data,
			success: function(data){
				if(data.status == '0'){
					if(data.data == 1){
						alert("您已加入过该社群");
	                	window.location.href= 'http://'+window.location.host+"/Index/myInvitation?community_id="+community_id+"&invite_id="+data.info;
	                }else if(data.data == 2){
	                	window.location.href= host+"/Index/only";
	                }else{
	                	//alert('恭喜你，社群加入成功！');
	                	//window.location.href= host+"/Index/getInvitation?community_id="+community_id+"&community_name="+data.info;
	                	//var usl= host+"/Index/getInvitation?community_id="+community_id+"&community_name="+data.info;
	                	publicPushNotice("恭喜你，社群加入成功！",community_id,data.info);
	                }
				}else{
	                alert(data.info);
	                if(data.data != ''){
	                	window.location.href = 'http://'+window.location.host+"/Index/myInvitation?community_id="+community_id+"&invite_id="+data.data;
	                }
	                return false;
	            }
	
	        },
	        dataType:"json"
	    });
	}
	
	//推送调用
	function publicPushNotice(notice,community_id,community_name){
		var host = 'http://'+window.location.host;
		var post_data={};
		$.ajax({
			type: "POST",
			url:"/Index/publicPushNotice",
			data: post_data,
			success: function(data){
				if(data.status == '0'){
					alert(notice);
					window.location.href = host+"/Index/getInvitation?community_id="+community_id+"&community_name="+community_name;

				}else{
	                alert(data.info);
	                return false;
	            }
	
	        },
	        dataType:"json"
	    });
	}
</script>